# coding=utf-8
import urllib, urllib2
from BeautifulSoup import BeautifulSoup
from sgmllib import SGMLParser

class URLLister(SGMLParser):
	def reset(self):
		SGMLParser.reset(self)
		self.urls = []

	def start_a(self, attrs):
		href = [v for k, v in attrs if k=='href']
		if href:
			self.urls.extend(href)




httpHandler = urllib2.HTTPHandler(debuglevel=1)
httpsHandler = urllib2.HTTPSHandler(debuglevel=1)

sourcebase = 'http://yuedukong.com/archive'
agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36'
post_data = ''

request = urllib2.Request(sourcebase, post_data)
request.add_header('User-Agent', agent)
opener = urllib2.build_opener()
html_data = opener.open(request).read().decode('utf-8')

# soup = BeautifulSoup(html_data)

i = 0
for year in range(2010, 2014):
    for month in range(01, 13):
        post_data = 'lite=1&month=' + str(year) + str(month)
        
        request = urllib2.Request(sourcebase, post_data)
        request.add_header('User-Agent', agent)
        
        opener = urllib2.build_opener()
        html_data = opener.open(request).read().decode('utf-8')

        parser = URLLister()
        parser.feed(html_data)
        parser.close()
        

        if i == 3:
            i = 0
            break
        if not len(parser.urls):
            i += 1
            continue
        print u'%s年%s月:' % (year, month)
        for url in parser.urls:
            print url
        tag = raw_input("Enter to continue('quit' to exit):")

